package com.example.blog.service.impl;

import com.example.blog.dao.ArticleDao;
import com.example.blog.dto.ArticleDTO;
import com.example.blog.pojo.Article;
import com.example.blog.pojo.PageBean;
import com.example.blog.service.ArticleService;
import com.example.blog.utils.ThreadLocalUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleDao articleDao;



    @Override
    public PageBean<ArticleDTO> getArticleList(Integer pageNum, Integer pageSize, Integer categoryId, Integer status) {
        //1.创建PageBean对象
        PageBean<ArticleDTO> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
//        Map<String,Object> map = ThreadLocalUtil.get();
//        Integer userId = (Integer) map.get("id");
        List<ArticleDTO> as = articleDao.list(categoryId,status);
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<ArticleDTO> p = (Page<ArticleDTO>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }


    @Override
    public Article getArticle(Integer articleid) {

        Article article = articleDao.findById(articleid);
        return article;
    }

    @Override
    public boolean addArticle(Article article) {
//        获取用户id
//        Map<String,Object> map = ThreadLocalUtil.get();
//        Integer userid = (Integer) map.get("id");
//        article.setUserid(userid);

        article.setCreatetime(LocalDateTime.now());
        article.setUpdatetime(LocalDateTime.now());
        article.setLikenumber(0);
        article.setMessage("等待管理员审核...");
        article.setStatus(0);

        Integer n = articleDao.addArticle(article);

        if(n>0)return true;
        else return false;
    }

    @Override
    public boolean updateArticle(Integer userid,Integer id, String title, String content,Integer category, String abstracts) {

        Integer n = articleDao.updateArticle(userid,id,title,content,abstracts,category,"等待管理员审核..",LocalDateTime.now(),0);
        if(n>0)return true;
        else return false;

    }

    @Override
    public boolean likeArticle(Integer articleid) {

        Integer n = articleDao.updateLikenumber(articleid);
        if(n>0)return true;
        else return false;

    }

    //    获取修改文章的作者id
    @Override
    public Integer getArticleUserid(Integer articleid) {

        Integer userid = articleDao.findUseridById(articleid);
        return userid;

    }

    @Override
    public List<ArticleDTO> getUserArticle(Integer userid,Integer status) {

        List<ArticleDTO> list = articleDao.findByUserid(userid,status);
        return list;

    }

    @Override
    public boolean deleteArticle(Integer id, Integer userid) {

        Integer n = articleDao.deleteById(id,userid);
        if(n>0)return true;
        else return false;

    }

}
